package com.lanswon.qzsmk.controller.cardManageController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.service.cardManageService.RefundService;
import com.lanswon.qzsmk.vo.QueryRefundInfoCondition;
import com.lanswon.qzsmk.vo.RefundDetailInfo;
import com.lanswon.qzsmk.vo.RefundInfo;
import com.lanswon.qzsmk.vo.VoucherVo;

@RequestMapping(value="/cancelRefund")
@Controller
public class CancelRefundController extends BaseController{
	
	@Autowired
	RefundService rs;
	
	@RequiresPermissions("cancelRefund:index")
	@RequestMapping(value="/index",method={RequestMethod.GET,RequestMethod.POST},name="联名卡注销退款,cancelRefund:index")
	public String goCancelRefund(){
		
		return "CancelRefund";
	}
	
	@RequiresPermissions("cancelRefund:getCancelRefundInfo")
	@RequestMapping(value="/getCancelRefundInfo",method=RequestMethod.POST,name="显示注销退款信息,cancelRefund:getCancelRefundInfo")
	@ResponseBody
	public DwzPage<RefundInfo> getCancelRefundInfo(QueryRefundInfoCondition cci,Integer pageNum,Integer numPerPage){
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;	
		}
		cci.setCurrentPage(pageNum);
		cci.setNumPerPage(numPerPage);
		cci.setStartLine((cci.getCurrentPage()-1)*cci.getNumPerPage());
		DwzPage<RefundInfo> dPage=rs.queryRefundList(cci);
		return dPage;
	}
	
	@RequiresPermissions("cancelRefund:refundRegister")
	@RequestMapping(value="/refundRegister",method=RequestMethod.POST,name="生成联名卡注销退款登记信息,cancelRefund:refundRegister")
	@ResponseBody
	public Map<String, String> refundRegister(String[]cancelCardReund,HttpServletRequest request){
		//长度只有一个
		Map<String, String> map=new HashMap<>();
		String [] strings=cancelCardReund[0].split("#");
		RefundInfo refundInfo=new RefundInfo();
		refundInfo.setCitizenCardNo(strings[0]);
		refundInfo.setCardpCodeName(strings[1]);
		refundInfo.setCardTypeName(strings[2]);
		refundInfo.setCustomerName(strings[3]);
		refundInfo.setPaperTypeName(strings[4]);
		refundInfo.setPaperNo(strings[5]);
		refundInfo.setCardStatusName(strings[6]);	
		Date date=new Date();
		date.setTime(Long.parseLong(strings[7]));
		refundInfo.setLogoutDate(date);
		System.out.println(date);
		request.getSession().setAttribute("refundInfo", refundInfo);
		map.put("statusCode", "200");
		map.put("callbackType", "forward");
		map.put("forwardUrl", "cancelRefund/goRefundRegisterDetail");//post请求	跳转退款登记
		return map;
	}
	
	@RequiresPermissions("cancelRefund:goRefundRegisterDetail")
	@RequestMapping(value="/goRefundRegisterDetail",method=RequestMethod.POST,name="去联名卡注销退款登记,cancelRefund:goRefundRegisterDetail")
	public String goRefundRegister(){
		
		return "CancelRefundRegister";
	}
	
	@RequiresPermissions("cancelRefund:goRefundRegisterDetail")
	@ResponseBody
	@RequestMapping(value="/getRefundRegisterInfo",method=RequestMethod.POST,name="获取联名卡注销登记信息,cancelRefund:goRefundRegisterDetail")
	public Map<String, Object> getRefundRegisterInfo(HttpServletRequest request) throws Exception{
		Map<String, Object> map=new HashMap<>();
		RefundInfo refundInfo=(RefundInfo) request.getSession().getAttribute("refundInfo");
		List<RefundDetailInfo>list=rs.queryDetailInfoByCitizenCardNo(refundInfo.getCitizenCardNo());
		map.put("refundInfo", refundInfo);
		map.put("list", list);
		map.put("statusCode", "200");
		return map;
	}
	
	@RequiresPermissions("cancelRefund:commitRefundInfo")
	@ResponseBody
	@RequestMapping(value="/commitRefundInfo",method=RequestMethod.POST,name="提交联名卡注销登记信息,cancelRefund:commitRefundInfo")
	public Map<String, String> commitRefundInfo(String citizenCardNo,String balance,HttpServletRequest request) throws ApplicationException{
		VoucherVo vv=rs.commitRefundInfo(citizenCardNo, balance);
		Map<String, String> map=new HashMap<>();
		//map.put("statusCode", "200");
		//map.put("callbackType", "forward");
		//map.put("forwardUrl", "cancelRefund/index");//post请求	跳转退款登记要改为跳转凭证
		request.getSession().setAttribute("vo", vv);
		request.getSession().setAttribute("type", "cancelRefund");
        map.put("title", "联名卡注销退款凭证");
        map.put("rel", "cancelRefund");
        map.put("statusCode", "200");
        map.put("url", "printCredentials/toHairpinPdfView");
        //20181204 hgl 增加以下4行
        map.put("max", "true");
        map.put("jumpToTile", "联名卡注销退款");
        map.put("jumpToTabid", "cancelRefund");
        map.put("jumpToUrl", "cancelRefund/index");
		return map;
	}

}
